package com.artisan.biz.factory;


import com.artisan.plugin.S3StoragePlugin;
import com.artisan.plugin.impl.FtpStoragePlugin;
import com.artisan.plugin.impl.LocalStoragePlugin;
import com.artisan.spi.StorePlugin;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ServiceLoader;

@Slf4j
public class PluginFactory {


    public static StorePlugin getTargetPlugin(String type) {

        ServiceLoader<StorePlugin> storePlugins = ServiceLoader.load(StorePlugin.class);

        for (StorePlugin storePlugin : storePlugins) {
            log.info("found plugin: {}", storePlugin.getClass().getName());
        }


        for (StorePlugin storePlugin : storePlugins) {
            if ("local".equals(type) && storePlugin instanceof LocalStoragePlugin) {
                return storePlugin;
            } else if ("ftp".equals(type) && storePlugin instanceof FtpStoragePlugin) {
                return storePlugin;
            } else if ("s3".equals(type) && storePlugin instanceof S3StoragePlugin) {
                return storePlugin;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        ServiceLoader<StorePlugin> serviceLoader = ServiceLoader.load(StorePlugin.class);
        serviceLoader.forEach(storePlugin -> storePlugin.saveFile(new File("/a")));
    }

}
